/**
 * 
 */
package tp.pr5.instructions;

import tp.pr5.NavigationModule;
import tp.pr5.RobotEngine;
import tp.pr5.instructions.exceptions.InstructionExecutionException;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;
import tp.pr5.items.ItemContainer;

/**
 * Its execution request the robot to finish the simulation This Instruction works if the user writes QUIT or SALIR
 *
 */
public class QuitInstruction implements Instruction {
	private RobotEngine engine;
	
	/**
	 * It builds a default QuitInstruction
	 */
	public QuitInstruction()
	{};
	

	@Override
	/**
	 * Parsers the String returning an instance of QuitInstruction or throwing a WrongInstructionFormatException()ç
	 * @param cad - text String to parse
	 * @return Instruction reference to an instance of QuitInstruction
	 */
	public Instruction parse(String cad) throws WrongInstructionFormatException
	{
		if (cad.equalsIgnoreCase("QUIT") || cad.equalsIgnoreCase("SALIR"))
		{
			return new QuitInstruction();
		}
		else
			throw new WrongInstructionFormatException(cad);
	}


	@Override
    /**
     * Returns a description of the Instruction syntax. The string does not end with the line separator. It is up to the caller adding it before printing.
     * @return The Instruction's syntax.
     */
	public String getHelp() {
		return "QUIT|SALIR";
	}


	@Override
	/**
	 * Set the execution context. The method receives the entire engine (engine, navigation and the robot container) even though the actual implementation of execute() may not require it.
	 * @param engine - The robot engine
	 * @param navigation - The information about the game, i.e., the places, current direction and current heading to navigate
	 * @param robotContainer - The inventory of the robot
	 */
	public void configureContext(RobotEngine engine, NavigationModule navigation, ItemContainer robotContainer) {
		this.engine = engine;
	}


	@Override
	/**
	 * Request the robot to stop the simulation
	 * @throws InstructionExecutionException - if there exist any execution error.
	 */
	public void execute() throws InstructionExecutionException {
		this.engine.requestQuit();
	}

}
